package com.ikun.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ikun.mapper.DishesMapper;
import com.ikun.pojo.Dishes;
import com.ikun.service.DishesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DishesServiceImpl implements DishesService {

    @Autowired
    DishesMapper dishesMapper;

    /**
     * 根据商家id查询商家的菜品
     *
     * @param businessId 商家的id
     * @return List<Dishes> 返回该商家所有的菜品
     */

    public List<Dishes> getDishesByBusinessId(Integer businessId) {
        //1.查询该商家所有的菜品
        List<Dishes> dishesList = dishesMapper.getDishesByBusinessId(businessId);
        //2.遍历所有的菜品:给每个菜品添加分类信息
        dishesList.forEach((dishes) -> {
            dishes.setDishesClass(new ArrayList<>());//给Dishes对象的新增项new一个对象
            dishes.setDishesDetails(new JSONObject());//给Dishes对象的新增项new一个对象
            //2.1给每个菜品添加分类
            Map<Integer, Object> category = dishesMapper.getDishesCategoryById(dishes.getDishesId());
            category.forEach((key, val) -> {
//                        System.out.println("类目：" + key + " 值：" + val);
                        //将java的Object转化为JSON字符串
                        String jsonString = JSON.toJSONString(val);
                        //将JSON字符串转化为JSON对象
                        JSONObject jsonObject = JSONObject.parseObject(jsonString);
                        //给菜品分类赋值
                        dishes.getDishesClass().add(jsonObject.getString("class_name"));
                    }
            );
            //2.2给每个菜品添加详情信息
            Map<Integer, Object> details = dishesMapper.getDishesDetailsById(dishes.getDishesId());
            details.forEach((key, val) -> {
//                        System.out.println("详情：" + key + " 值：" + val);
                        //将java的Object转化为JSON字符串
                        String jsonString = JSON.toJSONString(val);
                        //将JSON字符串转化为JSON对象
                        JSONObject jsonObject = JSONObject.parseObject(jsonString);
                        //给菜品详情赋值
                        dishes.getDishesDetails().put(jsonObject.getString("name"),jsonObject.getString("description"));

//                        System.out.println("获取键为name的值" + " " + jsonObject.getString("description"));
                    }
            );
        });

        return dishesList;
    }

    /**
     * 获取菜品分类信息
     *
     * @param dishesId 菜品ID
     * @return Map<String, Object> 返回包含菜品分类信息的Map
     */


    public Map<Integer, Object> getDishesCategoryById(Integer dishesId) {
        return dishesMapper.getDishesCategoryById(dishesId);
    }

    /**
     * 获取菜品的详情描述信息
     *
     * @param dishesId 菜品id
     * @return Map<String, Object>
     */

    public Map<Integer, Object> getDishesDetailsById(Integer dishesId) {
        return dishesMapper.getDishesDetailsById(dishesId);
    }
}
